package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseCity;
import com.maoye.ossp.sys.base.entity.SysBaseCountry;
import com.maoye.ossp.sys.base.entity.SysBaseFloor;
import com.maoye.ossp.sys.base.entity.SysBaseRegion;
import com.maoye.ossp.sys.base.entity.SysBaseShop;
import com.maoye.ossp.sys.base.entity.SysBaseState;
import com.maoye.ossp.sys.base.service.SysBaseCityService;
import com.maoye.ossp.sys.base.service.SysBaseCountryService;
import com.maoye.ossp.sys.base.service.SysBaseStateService;
import com.maoye.ossp.sys.site.utils.BeanToMap;

@Controller
@RequestMapping(value = "sys/sys/base/countryStateCity/")
public class SysBaseCountryStateCityController extends BaseController {
	@Resource
	private SysBaseCountryService sysBaseCountryService;
    @Resource 
	private SysBaseStateService sysBaseStateService ;
	@Resource 
	private SysBaseCityService sysBaseCityService ;
	/**
	 * 获得国家列表
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "getCountryList")
	public String getCountryList(HttpServletRequest request, Model model,
			SysBaseCountry country) {
		QueryFilter queryFilter = getQuerFilter(request,country);
		Page<SysBaseCountry> page = (Page<SysBaseCountry>)sysBaseCountryService
				.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/sys/base/country/getCountryList", model, request);
	}
	@RequestMapping(value = "addCountry")
	public String addCountry(HttpServletRequest request, Model model)
			throws IOException {
		Map map = new HashMap();
		SysBaseCountry sysBaseCountry = new SysBaseCountry();
		model.addAttribute("sysBaseCountry",sysBaseCountry);
		return "/sys/base/country/add";
	}
	
	/**
	 * 简称国家编码
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="checkCountryCode")
	public void checkCountryCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseCountryService.checkCountryCode(map)){
			json.put("resCode",0); //区域号可用
		}else{
			json.put("resCode",1);//区域号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
	/**
	 * 保存国家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "saveCountry")
	public String saveCountry(SysBaseCountry country,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		country.setId(idGenerator.getUId());
		country.setCode(country.getCode().replaceAll(",","").toString());
		//设置创建人
		country.setCreateBy("");
		//设置修改人
		country.setUpdateBy("");
		sysBaseCountryService.saveCountry(country);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getCountryList";
	}	
	/**
	 * 查看国家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="viewCountry")
	public String viewCountry(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseCountry country=null;
		country=sysBaseCountryService.getCountrybyCode(map);
		model.addAttribute("country",country);
		return "/sys/base/country/view";
	}
	@RequestMapping(value ="updateCountry")
	public String updateCountry(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseCountry sysBaseCountry=null;
		sysBaseCountry=sysBaseCountryService.getCountrybyCode(map);
		model.addAttribute("sysBaseCountry",sysBaseCountry);
		return "/sys/base/country/update";
	}
	/**
	 * 修改国家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="saveUpdateCountry")
	public String saveUpdateCountry(HttpServletRequest request,HttpServletResponse response,SysBaseCountry country,RedirectAttributes redirectAttributes){
		//设置创建人
		country.setCreateBy("");
		//设置修改人
		country.setUpdateBy("");
		sysBaseCountryService.saveUpdateCountry(country);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getCountryList";
	}
	/**
	 * 删除国家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="removeCountry")
	public String removeCountry(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		Map map = new HashMap();
		String code=request.getParameter("code");
		map.put("code",code);
		sysBaseCountryService.removeCountry(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getCountryList";
	}
	
	
	@RequestMapping(value = "getStateList")
	public String getStateList(HttpServletRequest request, Model model,
			SysBaseState state) {	
		QueryFilter queryFilter = getQuerFilter(request,state);
		Page<SysBaseState> page = (Page<SysBaseState>)sysBaseStateService
				.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/sys/base/state/getStateList", model, request);
	}
	
	
	@RequestMapping(value = "addState")
	public String addState(HttpServletRequest request, Model model)
			throws IOException {
		Map map = new HashMap();
		SysBaseState sysBaseState = new SysBaseState();
		model.addAttribute("sysBaseState",sysBaseState);
		return "/sys/base/state/add";
	}
	/**
	 * 保存省州信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "saveState")
	public String saveState(SysBaseState state,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		state.setId(idGenerator.getUId());
		state.setCode(state.getCode().replaceAll(",","").toString());
		//设置创建人
		state.setCreateBy("");
		//设置修改人
		state.setUpdateBy("");
		sysBaseStateService.saveState(state);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getStateList";
	}	
	@RequestMapping(value ="viewState")
	public String viewState(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String countryName=request.getParameter("countryName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseState state=null;
		state=sysBaseStateService.getStatebyCode(map);
		model.addAttribute("state",state);
		return "/sys/base/state/view";
	}
	
	/**
	 * 检查省州编码
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="checkStateCode")
	public void checkStateCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseStateService.checkStateCode(map)){
			json.put("resCode",0); //可用
		}else{
			json.put("resCode",1);//不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
	@RequestMapping(value ="updateState")
	public String updateState(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String countryName=request.getParameter("countryName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseState sysBaseState=null;
		sysBaseState=sysBaseStateService.getStatebyCode(map);
		model.addAttribute("sysBaseState",sysBaseState);
		model.addAttribute("countryName",countryName);
		return "/sys/base/state/update";
	}
	
	/**
	 * 修改省州信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="saveUpdateState")
	public String saveUpdateState(HttpServletRequest request,HttpServletResponse response,SysBaseState state,RedirectAttributes redirectAttributes){
		//设置创建人
		state.setCreateBy("");
		//设置修改人
		state.setUpdateBy("");
		sysBaseStateService.saveUpdateState(state);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getStateList";
	}
	
	
	/**
	 * 删除省州信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="removeState")
	public String removeState(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		Map map = new HashMap();
		String code=request.getParameter("code");
		map.put("code",code);
		sysBaseStateService.removeState(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getStateList";
	}
	
	@RequestMapping(value = "getCityList")
	public String getCityList(HttpServletRequest request, Model model,
			SysBaseCity city) {
		QueryFilter queryFilter = getQuerFilter(request,city);
		Page<SysBaseCity> page = (Page<SysBaseCity>)sysBaseCityService
				.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/sys/base/city/getCityList", model, request);
	}
	
	
	@RequestMapping(value = "addCity")
	public String addCity(HttpServletRequest request, Model model)
			throws IOException {
		Map map = new HashMap();
		SysBaseCity sysBaseCity = new SysBaseCity();
		model.addAttribute("sysBaseCity",sysBaseCity);
		return "/sys/base/city/add";
	}
	/*
	*//**
	 * 检查城市编码
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="checkCityCode")
	public void checkCityCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String code = request.getParameter("code");	
		map.put("code",code);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(sysBaseCityService.checkCityCode(map)){
			json.put("resCode",0); //城市编号可用
		}else{
			json.put("resCode",1);//城市编号不可以用
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	/*
	*//**
	 * 保存城市信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value = "saveCity")
	public String saveCity(SysBaseCity city,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws IOException{
		city.setId(idGenerator.getUId());
		city.setCode(city.getStateCode().replaceAll(",","").toString());
		//设置创建人
		city.setCreateBy("");
		//设置修改人
		city.setUpdateBy("");
		sysBaseCityService.saveCity(city);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:getCityList";
	}
	/*
	*//**
	 * 查看城市信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="viewCity")
	public String viewCity(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String stateName=request.getParameter("stateName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseCity city=null;
		city=sysBaseCityService.getCitybyCode(map);
		city.setStateName(stateName);
		model.addAttribute("city",city);
		return "/sys/base/city/view";
	}
	
	@RequestMapping(value ="updateCity")
	public String updateCity(HttpServletRequest request, HttpServletResponse response,Model model){
		String code =request.getParameter("code");
		String stateName=request.getParameter("stateName");
		Map map = new HashMap();
		map.put("code",code);
		SysBaseCity sysBaseCity=null;
		sysBaseCity=sysBaseCityService.getCitybyCode(map);
		model.addAttribute("sysBaseCity",sysBaseCity);
		model.addAttribute("stateName",stateName);
		return "/sys/base/city/update";
	}
	/*
	*//**
	 * 修改城市家信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */
	@RequestMapping(value ="saveUpdateCity")
	public String saveUpdateCity(HttpServletRequest request,HttpServletResponse response,SysBaseCity city,RedirectAttributes redirectAttributes){
		//设置创建人
		city.setCreateBy("");
		//设置修改人
		city.setUpdateBy("");
		sysBaseCityService.saveUpdateCity(city);
		addMessage(redirectAttributes, "修改成功");
		return "redirect:getCityList";
	}
	/*
	*//**
	 * 删除城市信息
	 * 
	 * @param request
	 * @param responser
	 * @throws IOException
	 */ 
	@RequestMapping(value ="removeCity")
	public String removeCity(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes){
		Map map = new HashMap();
		String code=request.getParameter("code");
		map.put("code",code);
		sysBaseCityService.removeCity(map);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:getCityList";
	}
}
